import java.io.*;
import java.net.*;

public class Cliente implements Runnable{
	private String serverHostname = new String ("127.0.0.1");
	private int port = 5000;
	private String nombre = "clienteSinNombre";
	
	
	public Cliente(String serverHostname, int port, String nombre) {
		super();
		this.serverHostname = serverHostname;
		this.port = port;
		this.nombre = nombre;
	}

	public Cliente() {
	}

	public void run() {
		
         System.out.println ("Intentando conexión del cliente "+nombre+" al servidor" +
                serverHostname + " en puerto "+ port);

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket(serverHostname, 5000);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("No conozco el servidor: " + serverHostname);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("No consigo entrado "
                               + "al servidor " + serverHostname);
            System.exit(1);
        }

		BufferedReader stdIn = new BufferedReader(
	                                   new InputStreamReader(System.in));
		String userInput="Hola servidor soy el cliente "+nombre;
		out.println(userInput);
		try {
			System.out.println(in.readLine());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		out.println("Bye.");
		try {
			System.out.println(in.readLine());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*System.out.println ("Type Message (\"Bye.\" to quit)");
        try {
		while ((userInput = stdIn.readLine()) != null) 
		       {
		    out.println(userInput);

		        // end loop
		        if (userInput.equals("Bye."))
		            break;

		    System.out.println("echo: " + in.readLine());
		   }
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}*/

	out.close();
	try {
		in.close();
		stdIn.close();
		echoSocket.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
		
	}
}
